<?php 
include('../../../../inc/application_settings.php');
include(FILESTORE);
include(IMAGE);


$email = $_COOKIE['EMAIL'];
$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$galleryId = (isset($_REQUEST['GALLERY_ID'])) ? $_REQUEST['GALLERY_ID'] : null;
$fileId = (isset($_REQUEST['FILE_ID'])) ? $_REQUEST['FILE_ID'] : null;
$imageId = (isset($_REQUEST['IMAGE_ID'])) ? $_REQUEST['IMAGE_ID'] : null;
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);
$response = array();

if ($auth->checkAuthentication()){
	switch ($target) {
		case 'UPLOAD':
			$response = uploadFile($galleryId);
			echo json_encode($response);
			break;
		case 'DOWNLOAD':
			downloadFile($fileId);
			break;
		case 'DELETE':
			deleteFile($fileId, $imageId);
			break;
	}
} 


/**
 * deletes the file from bio.gallery_image and from the filestore
 * @param 		int		 $fileId
 * @param 		int		 $imageId
 */
function deleteFile($fileId, $imageId){
	if (is_numeric($fileId) and is_numeric($imageId)){
		$sqlObj = Sql_Object::getInstance();
		$sqlObj->connect();
		
		$sql = 'delete from ' . DB_BIO . '.gallery_image where image_id = ?';
		$params = array($imageId);
		$sqlObj->execute($sql, $params);
		
		$filestore = new Filestore($sqlObj);
		$filestore->delete($fileId);
		$sqlObj->disconnect();
	}
}


/**
 * downloads a file with given id
 * @param 		int		 $fileId
 */
function downloadFile($fileId){
	if (is_numeric($fileId)){
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	
	$filestore = new Filestore($sqlObj);
	$filestore->enableContentEncryption(false);
	$filestore->download($fileId);
	
	$sqlObj->disconnect();
	}
}




/**
 * uploads a file and assign it to given gallery
 * @param 		int		 $galleryId
 */
function uploadFile($galleryId){
	$response = array('SUCCESS' => ERROR);
	
	if (is_numeric($galleryId)){
		$sqlObj = Sql_Object::getInstance();
		$sqlObj->connect();
		
		$filestore = new Filestore($sqlObj);
		$filestore->enableContentEncryption(false);
		$fileId = $filestore->upload(FILES_DIR, $_COOKIE['EMAIL']);

		if (is_numeric($fileId)){
			$imageId = createGalleryImage($galleryId, $fileId, $sqlObj, $filestore);
			if (is_numeric($imageId)){
				$response['IMAGE_ID'] = $imageId;
				$response['SUCCESS'] = SUCCESS;
			}
			else 
				$response['MSG'] = 'Image could not be created!';
		} else
			$response['MSG'] = 'Image could not be uploaded!';
		$sqlObj->disconnect();
	} else 
		$response['MSG'] = 'No valid gallery found!';
	
	return $response;
}



/**
 * makes an entry in bio.gallery_image
 * @param 		int		 $galleryId
 * @param 		int		 $fileId
 * @param 		mixed	 $sqlObj
 * @return		int		 $imageId
 */
function createGalleryImage($galleryId, $fileId, $sqlObj, $filestore){
 	$info = $filestore->getFileInfo($fileId);
 	$image = new Image();
 	$image->load($info['path'], $info['physicalname']);
	
	$sql = 'insert into ' . DB_BIO . '.gallery_image(gallery_id, file_id, active, height, width) values (?, ?, ?, ?, ?)';
	$params = array(
			$galleryId,
			$fileId,
			ACTIVE,
			$image->getHeight(),
			$image->getWidth()
			);
	
	$sqlObj->execute($sql, $params);
	$imageId = $sqlObj->getLastRecordId();
	
	$langObj = new Language($sqlObj);
	setName($langObj, $sqlObj, $imageId, APPLICATION_ID);
	return $imageId;
}


/**
 * registrates a image language key for name
 * @param 		mixed		 $langObj
 * @param 		mixed		 $sqlObj
 * @param 		int			 $imageId
 * @param 		int			 $applicationId
 */
function setName($langObj, $sqlObj, $imageId, $applicationId){
	$name_en = null;
	$name_de = null;

	$languageKeyId = $langObj->registrateKey('gallery_image_' . $imageId . '_name', $applicationId);
	setAttribute('name', $name_de, $imageId, $sqlObj, $applicationId, $langObj, $languageKeyId, GERMAN);
	setAttribute('name', $name_en, $imageId, $sqlObj, $applicationId, $langObj, $languageKeyId, ENGLISH);
}


/**
 * registrates a gallery image language value for given language and set entry in gallery_image table to langugage key
 * @param 		string		$column
 * @param 		string		$value
 * @param 		int			$imageId
 * @param 		mixed		$sqlObj
 * @param 		int			$applicationId
 * @param 		mixed		$langObj
 * @param 		int			$languageKeyId
 * @param 		string		$language
 */
function setAttribute($column, $value, $imageId, $sqlObj, $applicationId, $langObj, $languageKeyId, $language){
	$langObj->registrateValue($value, $languageKeyId, $language);

	$sql = 'update ' . DB_BIO . '.gallery_image set ' . $column . '_id = ? where image_id = ?';
	$params = array($languageKeyId, $imageId);
	$sqlObj->execute($sql, $params);
}


?>
